package com.fyh.util;

import com.fyh.constant.JwtConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

public class JwtUtil {

    private static final Long OUT_TIME = 1000L * 60 * 60 * 24;

    public static String createJwt(Long userId, String userType) {
        String token = Jwts.builder()
                .setSubject(String.valueOf(userId))
                .claim("userId", userId)
                .claim("userType", userType)
                .setExpiration(new Date(System.currentTimeMillis() + OUT_TIME))
                .signWith(SignatureAlgorithm.HS512, JwtConstant.SIGNATRUE)
                .compact();
        Claims claims = Jwts.parser()
                .setSigningKey(JwtConstant.SIGNATRUE)
                .parseClaimsJws(token)
                .getBody();
        Long id = ((Integer) claims.get("userId")).longValue();
        System.out.println("userId = " + id);

        return token;
    }
}
